﻿using Solution;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSLearn
{
    public class Node
    {
        public int val;
        public IList<Node> children;

        public Node() { }

        public Node(int _val)
        {
            val = _val;
        }

        public Node(int _val, IList<Node> _children)
        {
            val = _val;
            children = _children;
        }


        /// <summary>
        /// leetcode  429  N叉树的层次遍历
        /// </summary>
        /// <param name="root"></param>
        /// <returns></returns>
        public IList<IList<int>> LevelOrder(Node root)
        {
            IList<IList<int>> res = new List<IList<int>>();
            if(root != null)
            {
                Queue<Node> queue = new Queue<Node>();
                queue.Enqueue(root);
                int levelCount = 0;
                while (queue.Count > 0)
                {
                    levelCount = queue.Count;
                    IList<int> list = new List<int>();
                    for (int i = 0; i < levelCount; i++)
                    {
                        Node node = queue.Dequeue();
                        list.Add(node.val);

                        foreach (var item in node.children)
                        {
                            queue.Enqueue(item);
                        }
                    }
                    res.Add(list);
                }
            }

            return res;
        }
    }
}
